//
//  HYDeviceListApi.m
//  TJHYHardwareProduct
//
//  Created by JianWei Chen on 2018/1/23.
//  Copyright © 2018年 Kendama. All rights reserved.
//

#import "HYDeviceListApi.h"

@implementation HYDeviceListApi{
    NSDictionary *_dic;
}

-(id)initWithDic:(NSDictionary *)dic{
    if (self = [super init]) {
        _dic = dic;
    }
    return self;
}

- (NSString *)requestUrl {
    return @"/device/device_list";
}

- (YTKRequestMethod)requestMethod {
    return YTKRequestMethodPOST;
}

- (id)requestArgument {
    NSMutableDictionary *dic = [NSMutableDictionary getPubdic];
    
    [dic setValuesForKeysWithDictionary:_dic];
    NSString *sign=[NSString getSign:dic];
    [dic setObject:sign forKey:@"sign"];
    return dic;
}


-(void)dealloc
{
    NSLog(@"%@ dealloc",[self class]);
}

+ (void)getListPage:(int)page dno:(NSString *)dno province:(NSString *)province city:(NSString *)city district:(NSString *)district cid:(NSString *)cid child_cid:(NSString *)child_cid dong:(NSString *)dong ceng:(NSString *)ceng fang:(NSString *)fang success:(void (^)(NSArray<HYDeviceInfoModel *> *))successBack fail:(void (^)(NSDictionary *))failBack
{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setValue:@(page).stringValue forKey:@"page"];
    [requestDic setValue:dno forKey:@"dno"];
    [requestDic setValue:province forKey:@"province"];
    [requestDic setValue:city forKey:@"city"];
    [requestDic setValue:district forKey:@"district"];
    [requestDic setValue:cid forKey:@"cid"];
    [requestDic setValue:child_cid forKey:@"child_cid"];
    if (![dong isEqualToString:@""])
    {
        [requestDic setValue:dong forKey:@"dong"];
    }
    if (![ceng isEqualToString:@""])
    {
        [requestDic setValue:ceng forKey:@"ceng"];
    }
    if (![fang isEqualToString:@""])
    {
        [requestDic setValue:fang forKey:@"fang"];
    }
    
    HYDeviceListApi *api = [[HYDeviceListApi alloc]initWithDic:requestDic];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        NSMutableArray *deviceList = [[NSMutableArray alloc]init];
        NSDictionary *dic = request.responseObject;
        if (dic[@"data"] && dic[@"data"][@"list"]) {
            NSArray *list = dic[@"data"][@"list"];
            if ([list isKindOfClass:[NSArray class]]) {
                for (NSDictionary *infoDic in list) {
                    HYDeviceInfoModel *model = [[HYDeviceInfoModel alloc]init];
                    [model setValuesForKeysWithDictionary:infoDic];
                    [deviceList addObject:model];
                }
            }
        }
        if (successBack) {
            successBack(deviceList);
        }
    } failure:^(YTKBaseRequest * _Nonnull request) {
        if (failBack) {
            failBack(request.responseObject);
        }
    }];
}
@end
